/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.struts.service.beans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.struts.service.beans.JBFormField;
import opiam.admin.faare.struts.service.beans.JBRessourceRequest;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * Test of the JBRessourceRequest class.
 */
public class JBRessourceRequestTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBRessourceRequestTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Instance of JBRessourceRequest. */
    private JBRessourceRequest jbRessourceRequest = new JBRessourceRequest();

    /**
     * Creates a new JBRessourceRequestTest object.
     *
     * @param arg0  Parameter.
    */
    public JBRessourceRequestTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBRessourceRequestTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBRessourceRequestTest("testBegin"));

        suite.addTest(new JBRessourceRequestTest("testGetName"));
        suite.addTest(new JBRessourceRequestTest("testAddForm"));
        suite.addTest(new JBRessourceRequestTest("testFindFormByName"));

        suite.addTest(new JBRessourceRequestTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBRessourceRequestTest");

        _test.displayHeaderClass("JBRessourceRequestTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBRessourceRequestTest");
    }

    /**
     * Test the methods: setName and getName.
     *
     */
    public void testGetName()
    {
        _test.displayHeaderMethod("testGetName");

        try
        {
            String expected = "jbRessourceRequest";
            jbRessourceRequest.setName("jbRessourceRequest");

            String result = jbRessourceRequest.getName();

            if (expected == result)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetName");
    }

    /**
     * Test the methods: addForm and getFormMap.
     *
     */
    public void testAddForm()
    {
        _test.displayHeaderMethod("testAddForm");

        try
        {
            Map expectedFormMap = new HashMap();
            Map resultFormMap = new HashMap();

            JBFormField field1 = new JBFormField();
            field1.setName("field1");

            int expectedSize1 = expectedFormMap.size();
            expectedFormMap.put("field1", field1);

            int expectedSize2 = expectedFormMap.size();

            int resultSize1 = resultFormMap.size();
            jbRessourceRequest.addForm(field1);
            resultFormMap = jbRessourceRequest.getFormMap();

            int resultSize2 = resultFormMap.size();

            if (expectedSize2 == resultSize2)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddForm");
    }

    /**
     * Test the method: findFormByName.
     *
     */
    public void testFindFormByName()
    {
        _test.displayHeaderMethod("testFindFormByName");

        try
        {
            Map formMap = new HashMap();

            JBFormField field1 = new JBFormField();
            JBFormField field2 = new JBFormField();
            field1.setName("field1");
            jbRessourceRequest.addForm(field1);

            field2 = jbRessourceRequest.findFormByName("field1");

            if (field1.getName().equals(field2.getName()))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindFormByName");
    }
}
